//
//  LoginTypeView.m
//  PigMarket
//
//  Created by Felix on 2019/3/7.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "LoginTypeView.h"

#define Spacing 15

@interface LoginTypeView ()

/**
 sliderLine
 */
@property (nonatomic , strong) UILabel *sliderLine;
/**
 line
 */
@property (nonatomic , strong) UILabel *line;
/**
 loginTypeBottomLine
 */
@property (nonatomic , strong) UILabel *loginTypeBottomLine;

@end

@implementation LoginTypeView

#pragma mark - init method
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self addSubview:self.messageLogin];
    [self addSubview:self.sliderLine];
    [self addSubview:self.line];
    [self addSubview:self.passwordLogin];
    [self addSubview:self.loginTypeBottomLine];
}

- (void)initLayout {
    // 布局子控件
    [self.messageLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(Spacing);
        make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - 4 * Spacing)/2, 50));
    }];
    [self.passwordLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(-Spacing);
        make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - 4 * Spacing)/2, 50));
    }];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(-10);
        make.width.mas_equalTo(0.5);
    }];
    [self.sliderLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.messageLogin.mas_centerX);
        make.centerY.mas_equalTo(self.loginTypeBottomLine.mas_centerY);
        make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - 4 * Spacing)/8, 2));
    }];

    [self.loginTypeBottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Spacing * 2);
        make.top.mas_equalTo(self.messageLogin.mas_bottom);
        make.right.mas_equalTo(-Spacing * 2);
        make.height.mas_equalTo(0.5);
        make.bottom.mas_equalTo(0);
    }];
}

#pragma mark - getter and setter

- (void)setMessageLoginIsSelected:(BOOL)messageLoginIsSelected {
    _messageLoginIsSelected = messageLoginIsSelected;
    if (!self.messageLogin.selected && messageLoginIsSelected) {
        self.messageLogin.selected = YES;
        self.passwordLogin.selected = NO;
        @weakify(self);
        [UIView animateWithDuration:0.2 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            @strongify(self);
            self.sliderLine.transform = CGAffineTransformMakeTranslation(0, 0);
        } completion:^(BOOL finished) {
            
        }];
    }
}
- (void)setPasswordLoginIsSelected:(BOOL)passwordLoginIsSelected {
    _passwordLoginIsSelected = passwordLoginIsSelected;
    if (!self.passwordLogin.selected && passwordLoginIsSelected) {
        self.passwordLogin.selected = YES;
        self.messageLogin.selected = NO;
        
        @weakify(self);
        [UIView animateWithDuration:0.2 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            @strongify(self);
            self.sliderLine.transform = CGAffineTransformMakeTranslation(SCREEN_WIDTH/2, 0);
        } completion:^(BOOL finished) {
            
        }];
    }
}


- (UIButton *)messageLogin {
    if (!_messageLogin) {
        _messageLogin = [UIButton buttonWithType:UIButtonTypeCustom];
        [_messageLogin setTitle:@"短信登录" forState:UIControlStateNormal];
        [_messageLogin setTitleColor:[UIColor textGrayColor] forState:UIControlStateNormal];
        [_messageLogin setTitleColor:[UIColor primaryColor] forState:UIControlStateSelected];
        _messageLogin.titleLabel.font = mFont(16);
        _messageLogin.selected = YES;
    }
    return _messageLogin;
}
- (UILabel *)sliderLine {
    if (!_sliderLine) {
        _sliderLine = [[UILabel alloc] init];
        _sliderLine.backgroundColor = [UIColor primaryColor];
    }
    return _sliderLine;
}

- (UILabel *)line {
    if (!_line) {
        _line = [[UILabel alloc] init];
        _line.backgroundColor = [UIColor dividerColor];
    }
    return _line;
}
- (UIButton *)passwordLogin {
    if (!_passwordLogin) {
        _passwordLogin = [UIButton buttonWithType:UIButtonTypeCustom];
        [_passwordLogin setTitle:@"密码登录" forState:UIControlStateNormal];
        _passwordLogin.titleLabel.font = mFont(16);
        [_passwordLogin setTitleColor:[UIColor textGrayColor] forState:UIControlStateNormal];
        [_passwordLogin setTitleColor:[UIColor primaryColor] forState:UIControlStateSelected];
    }
    return _passwordLogin;
}

- (UILabel *)loginTypeBottomLine {
    if (!_loginTypeBottomLine) {
        _loginTypeBottomLine = [[UILabel alloc] init];
        _loginTypeBottomLine.backgroundColor = [UIColor dividerColor];
    }
    return _loginTypeBottomLine;
}

@end
